import unittest
from overlap import *


class TestOverLine(unittest.TestCase):
    def setUp(self):
        pass

    def test_vertical_line(self):
        line = OverLine(0, 1, 1, 5, 1, 4)
        self.assertEqual(line.kind, "y")
        self.assertEqual(line.k, 0.0)
        self.assertEqual(line.b, 0.0)
        self.assertEqual(line.minv, 0)
        self.assertEqual(line.maxv, 16)

    def test_horizontal_line(self):
        line = OverLine(1, 1, 1, 1, 3, 3)
        self.assertEqual(line.kind, "x")
        self.assertEqual(line.k, 0.0)
        self.assertEqual(line.b, 3.0)
        self.assertEqual(line.minv, 3)
        self.assertEqual(line.maxv, 11)

    def test_diagonal_line(self):
        """测试斜线的情况"""
        line = OverLine(1, 1, 1, 2, 2, 5)
        self.assertEqual(line.kind, "x")
        self.assertEqual(line.k, 1.0)
        self.assertEqual(line.b, 0.0)  # y = x
        self.assertEqual(line.minv, 3)
        self.assertEqual(line.maxv, 7)

    def test_negative_slope(self):
        line = OverLine(1, 5, 1, 2, 1, 1)
        self.assertEqual(line.kind, "x")
        self.assertEqual(line.k, -0.25)
        self.assertEqual(line.b, 6.75)  # y = -2x + 6
        self.assertEqual(line.minv, 3)
        self.assertEqual(line.maxv, 15)

    def test_rounding(self):
        """测试小数精度处理"""
        # 模拟需要四舍五入的情况
        line = OverLine(1, 1, 1, 2, 2, 2)
        self.assertEqual(line.kind, "x")
        self.assertEqual(line.k, 0.75)  # 四舍五入到5位小数
        self.assertEqual(line.b, 0.75)  # 四舍五入到5位小数
        self.assertEqual(line.minv, 3)
        self.assertEqual(line.maxv, 7)


class TestOverLine1(unittest.TestCase):
    def setUp(self):
        # 准备测试数据
        self.vertical_line1 = OverLine(0, 0, 1, 0, 0, 5)  # 垂直线 y=0, minv=0, maxv=5
        self.vertical_line2 = OverLine(0, 0, 3, 0, 0, 8)  # 垂直线 y=0, minv=3, maxv=8
        self.vertical_line3 = OverLine(
            0, 0, 10, 0, 0, 15
        )  # 垂直线 y=0, minv=10, maxv=15

        self.slope_line1 = OverLine(0, 0, 0, 1, 1, 1)  # 斜线 y=x, minv=0, maxv=1
        self.slope_line2 = OverLine(0, 0, 0, 2, 2, 2)  # 斜线 y=x, minv=0, maxv=2
        self.slope_line3 = OverLine(1, 1, 1, 3, 3, 3)  # 斜线 y=x, minv=1, maxv=3
        self.slope_line4 = OverLine(0, 1, 1, 1, 0, 0)  # 斜线 y=-x+1, minv=0, maxv=1

    def test_vertical_lines_overlap(self):
        """测试两条垂直线段有重叠的情况"""
        self.assertTrue(self.vertical_line1.isOverlap(self.vertical_line2))

    def test_vertical_lines_no_overlap(self):
        """测试两条垂直线段没有重叠的情况"""
        self.assertFalse(self.vertical_line1.isOverlap(self.vertical_line3))

    def test_slope_lines_overlap(self):
        """测试两条斜线段(相同斜率和截距)有重叠的情况"""
        self.assertTrue(self.slope_line1.isOverlap(self.slope_line2))
        self.assertTrue(self.slope_line2.isOverlap(self.slope_line3))

    def test_slope_lines_no_overlap(self):
        """测试两条斜线段(不同斜率和截距)没有重叠的情况"""
        self.assertFalse(self.slope_line1.isOverlap(self.slope_line4))

    def test_mixed_lines_no_overlap(self):
        """测试斜线和垂直线段没有重叠的情况"""
        self.assertFalse(self.vertical_line1.isOverlap(self.slope_line1))

    def test_same_slope_diff_intercept(self):
        """测试斜率相同但截距不同的线段不重叠"""
        line1 = OverLine(0, 0, 0, 1, 1, 1)  # y=x
        line2 = OverLine(0, 0, 1, 1, 1, 2)  # y=x+1
        self.assertFalse(line1.isOverlap(line2))

    def test_same_line_segments(self):
        """测试完全相同的线段"""
        line1 = OverLine(0, 0, 0, 1, 1, 1)
        line2 = OverLine(0, 0, 0, 1, 1, 1)
        self.assertTrue(line1.isOverlap(line2))


if __name__ == "__main__":
    unittest.main()
